package com.cgm.controller.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.csvreader.CsvReader;

/**
 * Created by lizg on 2017/4/25.
 */
public class CSVUtils {

	/**
	 * 导入
	 *
	 * @param file
	 *            csv文件(路径+文件)
	 * @return
	 */
	public static List<String> importCsv(File file) {
		List<String> dataList = new ArrayList<String>();

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				dataList.add(line);
			}
		} catch (Exception e) {
		} finally {
			if (br != null) {
				try {
					br.close();
					br = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return dataList;
	}


	public static void read(){

		String filePath = "D:/data/OrderDelivery2017-04-25 01-26-342.csv";

		try {
			// 创建CSV读对象
			CsvReader csvReader = new CsvReader(filePath);

			// 读表头
			csvReader.readHeaders();
			while (csvReader.readRecord()){
				// 读一整行
				System.out.println(csvReader.getRawRecord());
				// 读这行的某一列
				System.out.println(csvReader.get("Link"));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
		//read();

		List<String> dataList = importCsv(new File("D:/data/OrderDelivery2017-04-25 01-26-343.csv"));

		if (dataList != null && !dataList.isEmpty()) {
			for (int i = 1; i < dataList.size(); i++) {
				String data = dataList.get(i);
//				OrderDTO orderDTO = new OrderDTO();
				String[] source = data.split(",");
				if (source[0] != "") {
					System.out.println(source[1]);
					System.out.println(source[1].split("")[15].toString());
					System.out.println(source[15]);
					System.out.println(source[16]);
					//  orderService.modifyUnShipOrder(orderDTO);

					String orderSn = source[1];
					if(orderSn.contains("\"")){ //若发现有双引号  需要将字符串中的一个双引号替换为两个 并且需前后加双引号
						orderSn = source[1].replaceAll("\"","");
						System.out.println(orderSn);
					}
				}
			}
		}

	}
}
